package com.example.ass7.dataloader;

import java.awt.Color;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.example.ass7.helper.Convertor;

/**
 * @author Meirav Ben Izhak
 */
public class ColorsParser {
    // parse color definition and return the specified color.
    /**
     * @param s
     *            s
     * @return Color
     * @throws IOException
     *             IOException
     */
    public Background colorFromString(String s) throws IOException {

        Background bg = null;

        if (s == null
                || s.isEmpty()) {
            bg = new Background(null, Color.white);
            return bg;
        }

        if (s.startsWith("color(RGB(")) {
            String temp = s.substring(10).replace(")",
                    "");
            String[] parts = temp.split(",");
            if (parts != null
                    && parts.length == 3) {
                Color color = new Color(Convertor.parseInt(parts[0]),
                        Convertor.parseInt(parts[1]),
                        Convertor.parseInt(parts[2]));
                bg = new Background(null, color);
                return bg;
            }

        } else if (s.startsWith("color(")) {
            String temp = s.substring(6).replace(")",
                    "");
            if ("black".equalsIgnoreCase(temp)) {
                return new Background(null, Color.black);
            } else if ("blue".equalsIgnoreCase(temp)) {
                return new Background(null, Color.blue);
            } else if ("cyan".equalsIgnoreCase(temp)) {
                return new Background(null, Color.cyan);
            } else if ("gray".equalsIgnoreCase(temp)) {
                return new Background(null, Color.gray);
            } else if ("lightGray".equalsIgnoreCase(temp)) {
                return new Background(null, Color.lightGray);
            } else if ("green".equalsIgnoreCase(temp)) {
                return new Background(null, Color.green);
            } else if ("orange".equalsIgnoreCase(temp)) {
                return new Background(null, Color.orange);
            } else if ("pink".equalsIgnoreCase(temp)) {
                return new Background(null, Color.pink);
            } else if ("red".equalsIgnoreCase(temp)) {
                return new Background(null, Color.red);
            } else if ("white".equalsIgnoreCase(temp)) {
                return new Background(null, Color.white);
            } else if ("yellow".equalsIgnoreCase(temp)) {
                return new Background(null, Color.yellow);
            }
        } else if (s.startsWith("image(")) {
            String temp = s.substring(6).replace(")",
                    "");
            InputStream inputStream = ClassLoader.getSystemClassLoader()
                    .getResourceAsStream(temp);
            Image image = ImageIO.read(inputStream);
            return new Background(image, null);
        }
        return new Background(null, Color.white);
    }
}